package com.kevin.service;

import com.kevin.controller.PushMessageController;
import com.kevin.model.Location;
import com.kevin.model.ResBody;
import com.kevin.utils.PointsRep;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author kevin
 * @version V1.0
 * @Description: TODO
 * @date 2019/1/29 18:36
 **/
@Service
public class KmeansService {
    private static Logger LOGGER = LoggerFactory.getLogger(KmeansService.class);

    private static List<Location> list = Collections.synchronizedList(new ArrayList<>());

    @Autowired
    private MessPushService messPushService;

    @Autowired
    private CalculateData calculateData;

    @Autowired
    private PointsRep pointsRep;
    /**
     * 初始化地图打点
     */
    public void initMap(){
        /*
            封装推送界面展示数据
         */
        ResBody resBody = new ResBody();
        resBody.setType(0);
        resBody.setBody(encapsulateData());
        /*
            推送地图打点数据
         */
        messPushService.pushData(resBody);
    }

    /**
     * 执行聚类运算
     */
    public void executeCalculate(){
        LOGGER.info("开始聚类运算");
        Location[][] locations = calculateData.execute(10,encapsulateData());
        LOGGER.info("结束聚类运算");

        /*
            推送信息到前端界面
         */
        ResBody resBody = new ResBody();
        resBody.setType(1);
        resBody.setBody(locations);

        messPushService.pushData(resBody);
    }

    private List<Location> encapsulateData() {
        if(list.size()>0){
            return list;
        }else{
            list.add(new Location(1,115.511571000000004,38.906213999999999));
            list.add(new Location(1,115.467669000000001,38.901919999999997));
            list.add(new Location(1,115.509679000000006,38.890452000000003));
            list.add(new Location(1,115.507587000000001,38.906843000000002));
            list.add(new Location(1,115.501071999999994,38.887380000000000));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.505519000000007,38.890140000000002));
            list.add(new Location(1,115.458278000000007,38.855255999999997));
            list.add(new Location(1,115.458899000000002,38.860813000000000));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.451068000000006,38.858660000000000));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.485898000000006,38.849110000000003));
            list.add(new Location(1,115.492671000000001,38.846876000000002));
            list.add(new Location(1,115.526689000000005,38.862918000000001));
            list.add(new Location(1,115.487487999999999,38.863035000000004));
            list.add(new Location(1,115.509231999999997,38.893946000000000));
            list.add(new Location(1,115.506125999999995,38.874848000000000));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.471968000000004,38.874242000000002));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.455819000000005,38.915087999999997));
            list.add(new Location(1,115.472866999999994,38.873719999999999));
            list.add(new Location(1,115.467462999999995,38.826791000000000));
            list.add(new Location(1,115.483526999999995,38.877775999999997));
            list.add(new Location(1,115.458899000000002,38.860813000000000));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.457323000000002,38.892946999999999));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.516976999999997,38.908459999999998));
            list.add(new Location(1,115.486273999999995,38.909457000000003));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.457323000000002,38.892946999999999));
            list.add(new Location(1,115.469420999999997,38.876810999999996));
            list.add(new Location(1,115.469420999999997,38.876810999999996));
            list.add(new Location(1,115.406937999999997,38.916570999999998));
            list.add(new Location(1,115.454451000000006,38.903253999999997));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.495435000000001,38.872329000000001));
            list.add(new Location(1,115.465196000000006,38.879731999999997));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.458899000000002,38.860813000000000));
            list.add(new Location(1,115.504116999999994,38.881247000000002));
            list.add(new Location(1,115.467462999999995,38.826791000000000));
            list.add(new Location(1,115.501071999999994,38.887380000000000));
            list.add(new Location(1,115.554159999999996,38.879741000000003));
            list.add(new Location(1,115.518255999999994,38.891100000000002));
            list.add(new Location(1,115.518255999999994,38.891100000000002));
            list.add(new Location(1,115.499639999999999,38.914709000000002));
            list.add(new Location(1,115.499639999999999,38.914709000000002));
            list.add(new Location(1,115.468024000000000,38.902869000000003));
            list.add(new Location(1,115.498976999999996,38.891374999999996));
            list.add(new Location(1,115.463148000000004,38.886581000000000));
            list.add(new Location(1,115.440237999999994,38.886266999999997));
            list.add(new Location(1,115.481859999999998,38.881382000000002));
            list.add(new Location(1,115.422873999999993,38.912911999999999));
            list.add(new Location(1,115.467669000000001,38.901919999999997));
            list.add(new Location(1,115.486332000000004,38.896844000000002));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.547434999999993,38.908223000000000));
            list.add(new Location(1,115.498189999999994,38.951566000000000));
            list.add(new Location(1,115.498189999999994,38.951566000000000));
            list.add(new Location(1,115.595506000000000,38.883687000000002));
            list.add(new Location(1,115.595506000000000,38.883687000000002));
            list.add(new Location(1,115.483125000000001,38.897466999999999));
            list.add(new Location(1,115.523628000000002,38.804966999999998));
            list.add(new Location(1,115.572519000000000,38.849572999999999));
            list.add(new Location(1,115.554457999999997,38.813205000000004));
            list.add(new Location(1,115.595506000000000,38.883687000000002));
            list.add(new Location(1,115.547297000000000,38.882770999999998));
            list.add(new Location(1,115.517944999999997,38.943384000000002));
            list.add(new Location(1,115.544090999999995,38.885342000000001));
            list.add(new Location(1,115.503798000000003,38.832059000000001));
            list.add(new Location(1,115.471339999999998,38.859003999999999));
            list.add(new Location(1,115.533694999999994,38.820604000000003));
            list.add(new Location(1,115.549192000000005,38.831454000000001));
            list.add(new Location(1,115.503798000000003,38.832059000000001));
            list.add(new Location(1,115.519886000000000,38.840930000000000));
            list.add(new Location(1,115.471339999999998,38.859003999999999));
            list.add(new Location(1,115.554457999999997,38.813205000000004));
            list.add(new Location(1,115.563961000000006,38.861564999999999));
            list.add(new Location(1,115.563961000000006,38.861564999999999));
            list.add(new Location(1,115.563961000000006,38.861564999999999));
            list.add(new Location(1,115.552121999999997,38.805200999999997));
            list.add(new Location(1,115.552121999999997,38.805200999999997));
            list.add(new Location(1,115.580654999999993,38.904631000000002));
            list.add(new Location(1,115.580654999999993,38.904631000000002));
            list.add(new Location(1,115.580654999999993,38.904631000000002));
            list.add(new Location(1,115.580654999999993,38.904631000000002));
            list.add(new Location(1,115.526262000000003,38.875171999999999));
            list.add(new Location(1,115.526262000000003,38.875171999999999));
            list.add(new Location(1,115.544090999999995,38.885342000000001));
            list.add(new Location(1,115.544090999999995,38.885342000000001));
            list.add(new Location(1,115.511495999999994,38.898614999999999));
            list.add(new Location(1,115.495687000000004,38.902307999999998));
            list.add(new Location(1,115.477705000000000,38.897002000000001));

            //保定数据
            /*list.add(new Location(1,116.691990000000000,39.544080000000001));
            list.add(new Location(1,116.691940000000000,39.544040000000001));*/

            /*list.add(new Location(1,116.424391,39.952543));
            list.add(new Location(1,116.424463,39.950717));
            list.add(new Location(1,116.424103,39.948892));
            list.add(new Location(1,116.428846,39.954202));
            list.add(new Location(1,116.430356,39.951436));
            list.add(new Location(1,116.437398,39.948947));
            list.add(new Location(1,116.431649,39.948781));
            list.add(new Location(1,116.434452,39.951104));
            list.add(new Location(1,116.433805,39.948394));
            list.add(new Location(1,116.426619,39.929087));
            list.add(new Location(1,116.426547,39.924937));
            list.add(new Location(1,116.426187,39.924107));
            list.add(new Location(1,116.433518,39.918961));
            list.add(new Location(1,116.428415,39.918131));
            list.add(new Location(1,116.433015,39.922447));
            list.add(new Location(1,116.427912,39.917024));
            list.add(new Location(1,116.435278,39.926652));
            list.add(new Location(1,116.437003,39.924052));
            list.add(new Location(1,116.435422,39.923609));
            list.add(new Location(1,116.464168,39.952321));
            list.add(new Location(1,116.464455,39.949832));
            list.add(new Location(1,116.463521,39.948781));
            list.add(new Location(1,116.463162,39.948007));
            list.add(new Location(1,116.470348,39.953151));
            list.add(new Location(1,116.468552,39.950607));
            list.add(new Location(1,116.471282,39.950219));
            list.add(new Location(1,116.469917,39.948394));
            list.add(new Location(1,116.468552,39.950607));
            list.add(new Location(1,116.467114,39.951658));
            list.add(new Location(1,116.497657,39.953787));
            list.add(new Location(1,116.499166,39.953621));
            list.add(new Location(1,116.503837,39.952626));
            list.add(new Location(1,116.504699,39.956497));
            list.add(new Location(1,116.499597,39.951021));
            list.add(new Location(1,116.505993,39.956774));
            list.add(new Location(1,116.499597,39.950745));
            list.add(new Location(1,116.506855,39.949528));
            list.add(new Location(1,116.505634,39.948588));
            list.add(new Location(1,116.505993,39.956995));
            list.add(new Location(1,116.533086,39.954174));
            list.add(new Location(1,116.532655,39.953179));
            list.add(new Location(1,116.532655,39.951851));
            list.add(new Location(1,116.532655,39.950081));
            list.add(new Location(1,116.532726,39.948975));
            list.add(new Location(1,116.536392,39.954506));
            list.add(new Location(1,116.537613,39.952736));
            list.add(new Location(1,116.537613,39.952736));
            list.add(new Location(1,116.537685,39.950302));
            list.add(new Location(1,116.537685,39.950302));
            list.add(new Location(1,116.568012,39.955889));
            list.add(new Location(1,116.568874,39.954506));
            list.add(new Location(1,116.568012,39.953289));
            list.add(new Location(1,116.568227,39.951685));
            list.add(new Location(1,116.571964,39.954119));
            list.add(new Location(1,116.571677,39.953842));
            list.add(new Location(1,116.571677,39.953842));
            list.add(new Location(1,116.576204,39.954893));
            list.add(new Location(1,116.574623,39.953179));
            list.add(new Location(1,116.575917,39.951519));
            list.add(new Location(1,116.460503,39.927731));
            list.add(new Location(1,116.459784,39.924854));
            list.add(new Location(1,116.458994,39.922309));
            list.add(new Location(1,116.472432,39.922917));
            list.add(new Location(1,116.464024,39.924024));
            list.add(new Location(1,116.472001,39.926514));
            list.add(new Location(1,116.467977,39.922917));
            list.add(new Location(1,116.464024,39.924024));
            list.add(new Location(1,116.471354,39.925407));
            list.add(new Location(1,116.469198,39.920095));
            list.add(new Location(1,116.500316,39.928229));
            list.add(new Location(1,116.500675,39.926293));
            list.add(new Location(1,116.502328,39.925573));
            list.add(new Location(1,116.504053,39.927399));
            list.add(new Location(1,116.506783,39.925518));
            list.add(new Location(1,116.498232,39.923139));
            list.add(new Location(1,116.502328,39.924688));
            list.add(new Location(1,116.505849,39.928285));
            list.add(new Location(1,116.498806,39.924633));
            list.add(new Location(1,116.500244,39.924079));
            list.add(new Location(1,116.536032,39.929059));
            list.add(new Location(1,116.538655,39.924771));
            list.add(new Location(1,116.535924,39.924716));
            list.add(new Location(1,116.544476,39.928644));
            list.add(new Location(1,116.542248,39.926708));
            list.add(new Location(1,116.545554,39.927704));
            list.add(new Location(1,116.538655,39.925712));
            list.add(new Location(1,116.546848,39.927704));
            list.add(new Location(1,116.544261,39.925546));
            list.add(new Location(1,116.537218,39.926818));
            list.add(new Location(1,116.570922,39.928534));
            list.add(new Location(1,116.570347,39.928036));
            list.add(new Location(1,116.572503,39.928589));
            list.add(new Location(1,116.575378,39.929087));
            list.add(new Location(1,116.572539,39.929253));
            list.add(new Location(1,116.571929,39.924826));
            list.add(new Location(1,116.575234,39.929198));
            list.add(new Location(1,116.568731,39.927482));
            list.add(new Location(1,116.573078,39.926708));
            list.add(new Location(1,116.573797,39.927206));*/

            for(int i=0;i<list.size();i++){
                list.get(i).setId(i);
            }
        }

        return list;
    }
}
